package ca.usherbrooke.builder.equipment;

import java.io.FileNotFoundException;
import java.io.IOException;

import ca.usherbrooke.model.damage.ConstantDamage;
import ca.usherbrooke.model.damage.Damage;
import ca.usherbrooke.model.damage.NullDamage;
import ca.usherbrooke.model.damage.RandomDamage;
import ca.usherbrooke.view.properties.PropertieBase;

public class WeaponModelObjectBuilder {

	// name keys
	private final static String RANGE = "weapon_model_range_key";

	public static WeaponModelObject createWeaponObject(String fileName)
			throws FileNotFoundException, IOException {
		PropertieBase properties = new PropertieBase(fileName);

		WeaponModelObject equipmentObject = new WeaponModelObject();

		EquipmentModelObjectBuilder.fillEquipmentObject(properties,
				equipmentObject);

		equipmentObject.range = properties
				.getFloatValue(WeaponModelObjectBuilder.RANGE);
		equipmentObject.damage = createDamage(properties);

		return equipmentObject;
	}

	private final static String DAMAGE_TYPE = "damage_type_key";

	private final static String DAMAGE_TYPE_CONSTANT = "constant";
	private final static String DAMAGE_TYPE_CONSTANT_VALUE = "damage_value_key";

	private final static String DAMAGE_TYPE_RANDOM = "random";
	private final static String DAMAGE_TYPE_RANDOM_DICE_COUNT = "damage_dice_count_key";
	private final static String DAMAGE_TYPE_RANDOM_DICE_VALUE = "damage_dice_key";

	public static Damage createDamage(PropertieBase properties) {

		Damage dmg = new NullDamage();

		String type = properties.getValue(DAMAGE_TYPE);

		if (DAMAGE_TYPE_CONSTANT.equals(type)) {
			int val = properties.getIntegerValue(DAMAGE_TYPE_CONSTANT_VALUE);
			dmg = new ConstantDamage(val);
		} else if (DAMAGE_TYPE_RANDOM.equals(type)) {
			int diceCount = properties
					.getIntegerValue(DAMAGE_TYPE_RANDOM_DICE_COUNT);
			int dice = properties
					.getIntegerValue(DAMAGE_TYPE_RANDOM_DICE_VALUE);
			dmg = new RandomDamage(diceCount, dice);
		}
		return dmg;
	}
}
